<style scoped lang="scss">
@import url('index.scss');
</style>

<template>
  <div class="app-container">
     <!--新模版样式-->
    <div class="topsearch">
      <el-row>
        <el-col :span="22" class="searchcol">
<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="入库单" prop="inid">
        <el-input
          v-model="queryParams.inid"
          placeholder="请输入入库单"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品明细" prop="itemid">
        <el-input
          v-model="queryParams.itemid"
          placeholder="请输入药品明细"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存放位置" prop="storageLoc">
        <el-input
          v-model="queryParams.storageLoc"
          placeholder="请输入存放位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库活度" prop="initialActivity">
        <el-input
          v-model="queryParams.initialActivity"
          placeholder="请输入入库活度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前活度" prop="currentActivity">
        <el-input
          v-model="queryParams.currentActivity"
          placeholder="请输入当前活度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后计算时间" prop="measureTime">
        <el-date-picker clearable
          v-model="queryParams.measureTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择最后计算时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="物理半衰期(分)" prop="halfLifemin">
        <el-input
          v-model="queryParams.halfLifemin"
          placeholder="请输入物理半衰期(分)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="itemnum">
        <el-input
          v-model="queryParams.itemnum"
          placeholder="请输入数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button  class="newButton" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22" class="rightcol">
          <div class="newbtn" @click="handleAdd" v-hasPermi="['system:stocks:add']">
            <img src="@/assets/images/add.svg" alt="">
            <span>新建</span>
          </div>
          <div class="newbtn" v-if="single == false" @click="handleUpdate" v-hasPermi="['system:stocks:edit']">
            <img src="@/assets/images/edit.svg" alt=""></img>
            <span>编辑</span>
          </div>
          <div class="newbtn" v-if="multiple == false" @click="handleDelete" v-hasPermi="['system:stocks:remove']">
            <img src="@/assets/images/del.svg" alt=""></img>
            <span>删除</span>
          </div>
          <div class="newbtn" @click="handleExport" v-hasPermi="['system:stocks:export']">
            <img src="@/assets/images/export.svg" alt=""></img>
            <span>导出</span>
          </div>
          <div class="newbtn" @click="getList">
            <img src="@/assets/images/refresh.svg" alt=""></img>
            <span>刷新</span>
          </div>
        </el-col>
      </el-row>
    </div>

    
<!-- 
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:stocks:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:stocks:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:stocks:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:stocks:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="stocksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="库存ID" align="center" prop="stockid" />
      <el-table-column label="入库单" align="center" prop="inid" />
      <el-table-column label="药品明细" align="center" prop="itemid" />
      <el-table-column label="存放位置" align="center" prop="storageLoc" />
      <el-table-column label="入库活度" align="center" prop="initialActivity" />
      <el-table-column label="当前活度" align="center" prop="currentActivity" />
      <el-table-column label="最后计算时间" align="center" prop="measureTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.measureTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="物理半衰期(分)" align="center" prop="halfLifemin" />
      <el-table-column label="数量" align="center" prop="itemnum" />
      <el-table-column label="库存状态" align="center" prop="stockStatus" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:stocks:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:stocks:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改实时库存对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" class="newDialog">
      <el-form ref="stocksRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="入库单" prop="inid">
          <el-input v-model="form.inid" placeholder="请输入入库单" />
        </el-form-item>
        <el-form-item label="药品明细" prop="itemid">
          <el-input v-model="form.itemid" placeholder="请输入药品明细" />
        </el-form-item>
        <el-form-item label="存放位置" prop="storageLoc">
          <el-input v-model="form.storageLoc" placeholder="请输入存放位置" />
        </el-form-item>
        <el-form-item label="入库活度" prop="initialActivity">
          <el-input v-model="form.initialActivity" placeholder="请输入入库活度" />
        </el-form-item>
        <el-form-item label="当前活度" prop="currentActivity">
          <el-input v-model="form.currentActivity" placeholder="请输入当前活度" />
        </el-form-item>
        <el-form-item label="最后计算时间" prop="measureTime">
          <el-date-picker clearable
            v-model="form.measureTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择最后计算时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="物理半衰期(分)" prop="halfLifemin">
          <el-input v-model="form.halfLifemin" placeholder="请输入物理半衰期(分)" />
        </el-form-item>
        <el-form-item label="数量" prop="itemnum">
          <el-input v-model="form.itemnum" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Stocks">
import { listStocks, getStocks, delStocks, addStocks, updateStocks } from "@/api/system/stocks"

const { proxy } = getCurrentInstance()

const stocksList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    inid: null,
    itemid: null,
    storageLoc: null,
    initialActivity: null,
    currentActivity: null,
    measureTime: null,
    halfLifemin: null,
    itemnum: null,
    stockStatus: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询实时库存列表 */
function getList() {
  loading.value = true
  listStocks(queryParams.value).then(response => {
    stocksList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    stockid: null,
    inid: null,
    itemid: null,
    storageLoc: null,
    initialActivity: null,
    currentActivity: null,
    measureTime: null,
    halfLifemin: null,
    itemnum: null,
    stockStatus: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("stocksRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.stockid)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加实时库存"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _stockid = row.stockid || ids.value
  getStocks(_stockid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改实时库存"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["stocksRef"].validate(valid => {
    if (valid) {
      if (form.value.stockid != null) {
        updateStocks(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addStocks(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _stockids = row.stockid || ids.value
  proxy.$modal.confirm('是否确认删除实时库存编号为"' + _stockids + '"的数据项？').then(function() {
    return delStocks(_stockids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/stocks/export', {
    ...queryParams.value
  }, `stocks_${new Date().getTime()}.xlsx`)
}

getList()
</script>
